package com.lwj.servletAPI;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // 设置响应内容类型
        response.setContentType("text/html");
        // 要重定向的新位置
        String site = new String("http://www.w3cschool.cn");
        response.setStatus(response.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", site);
    }

    //转发， 路径只能是内部
    public String test2(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/test4");
        requestDispatcher.forward(request,response);
        return "test2";
    }
    //重定向，路径可以是外部，也可以是内部
    public String test3(HttpServletResponse response) throws IOException {
//        response.sendRedirect("https://www.baidu.com/");
        response.sendRedirect("/test4");
        return "test3";
    }
    public String test4(){
        return "test4";
    }

    public String test5(HttpServletResponse response) throws IOException {
        // 设置响应内容类型
        response.setContentType("text/html");
        // 要重定向的新位置
        String site = new String("/test4");
        response.setStatus(response.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", site);

        // response.sendRedirect("/test4");HttpServletResponse的重定向方法也是修改响应头
        return null;
    }
}
